package psn.zjk.springboot.dubbo.server;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import psn.zjk.springboot.dubbo.api.Service;

/**
 * @USER white
 * @DATETIME 2020/11/21 22:24
 */
@Configuration
public class DubboServerConfig {

    @Bean
    public ProtocolConfig protocol() {
        ProtocolConfig protocol = new ProtocolConfig();
        protocol.setName("dubbo");
        protocol.setPort(12345);
        protocol.setThreads(200);
        return protocol;
    }

    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("10.20.130.230:9090");
        registry.setUsername("aaa");
        registry.setPassword("bbb");
        return registry;
    }

    public ServiceConfig<Service> service(){
        ApplicationConfig application = new ApplicationConfig();
        ServiceConfig<Service> service = new ServiceConfig<Service>(); // 此实例很重，封装了与注册中心的连接，请自行缓存，否则可能造成内存和连接泄漏
//        service.setApplication(application);
//        service.setRegistry(registry); // 多个注册中心可以用setRegistries()
//        service.setProtocol(protocol); // 多个协议可以用setProtocols()
//        service.setInterface(Service.class);
//        service.setRef(service);
//        service.setVersion("1.0.0");

// 暴露及注册服务
        service.export();
        return service;
    }

 
}
